package com.nf.xiaomi.dao.impl;

import com.nf.dbutils.SqlExecutor;
import com.nf.dbutils.handlers.BeanListHandler;
import com.nf.xiaomi.dao.EvaluateDao;
import com.nf.xiaomi.entity.EvaluateEntity;
import com.nf.xiaomi.util.SqlExecutorUtils;

import java.time.LocalDateTime;
import java.util.List;

public class EvaluateDaoImpl implements EvaluateDao {
    SqlExecutor executor = SqlExecutorUtils.getExecutor();
    @Override
    public List<EvaluateEntity> getAll() {
        String sql = "select e.evaluate_id,pd.product_detail_id,u.user_account,u.user_name,e.evaluate_content,e.evaluate_date from evaluate e\n" +
                "left join order_info o on o.order_id = e.order_id\n" +
                "left join order_detail od on od.order_details_id = e.order_details_id\n" +
                "left join product_detail pd on pd.product_detail_id = od.product_detail_id\n" +
                "left join user_info u on u.user_id = e.user_id";
        BeanListHandler<EvaluateEntity> handler = new BeanListHandler<>(EvaluateEntity.class);
        return executor.query(sql, handler);
    }

    @Override
    public List<EvaluateEntity> getAll(Integer userID) {
        String sql = "select e.evaluate_id,pd.product_detail_id,u.user_account,u.user_name,e.evaluate_content,e.evaluate_date from evaluate e\n" +
                "left join order_info o on o.order_id = e.order_id\n" +
                "left join order_detail od on od.order_details_id = e.order_details_id\n" +
                "left join product_detail pd on pd.product_detail_id = od.product_detail_id\n" +
                "left join user_info u on u.user_id = e.user_id where e.user_id = ?";
        BeanListHandler<EvaluateEntity> handler = new BeanListHandler<>(EvaluateEntity.class);
        return executor.query(sql, handler,userID);
    }

    @Override
    public List<EvaluateEntity> getAllEvaluateID(Integer evaluateID) {
        String sql = "select e.evaluate_id,pd.product_detail_id,u.user_account,u.user_name,e.evaluate_content,e.evaluate_date from evaluate e\n" +
                "left join order_info o on o.order_id = e.order_id\n" +
                "left join order_detail od on od.order_details_id = e.order_details_id\n" +
                "left join product_detail pd on pd.product_detail_id = od.product_detail_id\n" +
                "left join user_info u on u.user_id = e.user_id where e.evaluate_id = ?";
        BeanListHandler<EvaluateEntity> handler = new BeanListHandler<>(EvaluateEntity.class);
        return executor.query(sql, handler,evaluateID);
    }

    @Override
    public int insert(Integer orderDetailsId,String evaluateContent, LocalDateTime evaluateDate, Integer nstate) {
        String sql = "insert into evaluate(order_details_id,evaluate_content, evaluate_date, nstate)\n" +
                "values (?,?,?,?);";
        return executor.update(sql, orderDetailsId,evaluateContent, evaluateDate, nstate);
    }

    @Override
    public int update(String evaluateContent, LocalDateTime evaluateDate, Integer evaluateId) {
        String sql = "update evaluate set evaluate_content=? , evaluate_date=?\n" +
                "where evaluate_id=?";
        return executor.update(sql, evaluateContent, evaluateDate, evaluateId);
    }

    @Override
    public int delete(Integer evaluateId) {
        String sql = "delete from evaluate where evaluate_id=?";
        return executor.update(sql, evaluateId);
    }
}
